package org.jobtask.schedule;

import java.util.Date;
import java.util.Observable;
import org.jobtask.config.JobTaskConfig;
import org.jobtask.config.ObjectFactory;
import org.jobtask.config.SystemIdentification;
import org.jobtask.iface.Scheduling;
import org.jobtask.listener.Monitor;

public class Nurse extends Thread{
	
	private Monitor mthis=null;
	private boolean isHalt=false;
	
	public Nurse(Monitor mon){
		this.mthis=mon;
	}
	
	@Override
	public void run() {
			for(;!isHalt;){
				this.mthis.getComponents().getInfoLab().setText("["+SystemIdentification.parseDateTimeToString(new Date(),true)+"]开始监控>>"+ExceptionJobStack.getJobSize());
				if(ExceptionJobStack.isEmpty()){
					try {
						Thread.sleep(30*1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}else{
					Object [] jobs=ExceptionJobStack.getJobs();
					for(int index=0;index<jobs.length;index++){
						JobTask jt=(JobTask)jobs[index];
						Scheduling scheduling=(Scheduling)ObjectFactory.getInstance(JobTaskConfig.getString(jt.getTaskType()), jt);
						if(scheduling instanceof Observable){((Observable) scheduling).addObserver(this.mthis);}
						new Thread(scheduling);
						mthis.getTaskStack().put(jt.getSequece(),scheduling);
						mthis.getComponents().getText().setDocument(jt.getLogDocument());
						mthis.getComponents().startButtonStateChange();
						mthis.getComponents().changeTableProperty(true);
						mthis.setStatusTaskScript();
					}
					ExceptionJobStack.clearStopJob();
			}
		}
			this.mthis.getComponents().getInfoLab().setText("监控停止>>"+ExceptionJobStack.getJobSize());
	}
	
	public  void halt(){
		this.mthis.getComponents().getInfoLab().setText("监控停止>>"+ExceptionJobStack.getJobSize());
		this.interrupt();
		isHalt=true;
	}
	
}
